import mx.utils.Delegate;

/*
	mouse monitor
	version 0.9
	Tophy 2008/1/29
*/


dynamic class MouseListener{
	public static var M_MOUSEDOWN:String="onmousedown";
	public static var M_MOUSEUP:String="onmouseup";
	public static var M_DOUBLECLICK:String="ondoubleclick";
	public static var M_MOUSEMOVE:String="onmousemove";
	
	
	private var $mouseObject:Object;
	private var $parent:MovieClip;
	private var $xDragDis:Number=0;
	private var $yDragDis:Number=0;
	private var $doubleclickTime:Number=300;
	
	public function get DoubleClick():Boolean{
		return this.$mouseObject.dc;
	}
	public function get MouseDown():Boolean{
		return this.$mouseObject.md;
	}
	public function get X():Number{
		return this.$mouseObject.x;
	}
	public function get Y():Number{
		return this.$mouseObject.y;
	}
	public function get DoubleClickTime():Number{
		return this.$doubleclickTime;
	}
	public function set DoubleClickTime(value:Number){
		if(value<=0) return;
		this.$doubleclickTime=value;
	}
	public function get MouseDownTime():Number{
		return this.$mouseObject.mdtime;
	}
	public function MouseListener(_mc:MovieClip){
		if(_mc==undefined){
			_mc=_root;
		}
		this.$parent=_mc;
		initVars();
		initBroadcaster();
	}
	private function initBroadcaster(){
		AsBroadcaster.initialize(this);
	}
	private function initVars(){
		this.$mouseObject=new Object();
		this.Reset();
	}
	private function setMouseEvents(){
		if(!checkWork()) return;
		this.$mouseObject.dc=false;
		this.$mouseObject.md=false;
		this.$mouseObject.mdtime=0;
		this.$mouseObject.x=0;
		this.$mouseObject.y=0;
		this.$mouseObject.onMouseDown=Delegate.create(this,this.mousedownfun);
		this.$mouseObject.onMouseUp=Delegate.create(this,this.mouseupFun);
		this.$mouseObject.onMouseMove=Delegate.create(this,this.mousemoveFun);
		
		Mouse.addListener(this.$mouseObject);
	}
	private function mousedownfun(){
		if(!checkWork()){
			Mouse.removeListener(this.$mouseObject);
			return;
		}
		this.$mouseObject.md=true;
		this.$xDragDis+=(this.$parent._xmouse-this.$mouseObject.x);
		this.$yDragDis+=(this.$parent._ymouse-this.$mouseObject.y);
		this.$mouseObject.x=this.$parent._xmouse;
		this.$mouseObject.y=this.$parent._ymouse;
		if(((new Date()).getTime()-this.$mouseObject.mdtime)<this.$doubleclickTime){
			this.$mouseObject.dc=true;
		}
		this.$mouseObject.mdtime=(new Date()).getTime();
		this.broadcastMessage(M_MOUSEDOWN,this);
		if(this.$mouseObject.dc){
			this.broadcastMessage(M_DOUBLECLICK,this);
		}
	}
	private function mouseupFun(){
		if(!checkWork()){
			Mouse.removeListener(this.$mouseObject);
			return;
		}
		this.$mouseObject.dc=false;
		this.$mouseObject.md=false;
		this.broadcastMessage(M_MOUSEUP,this);
	}
	private function mousemoveFun(){
		if(!checkWork()){
			Mouse.removeListener(this.$mouseObject);
			return;
		}
		var tempx:Number=this.$mouseObject.x;
		var tempy:Number=this.$mouseObject.y;
		this.$mouseObject.x=this.$parent._xmouse;
		this.$mouseObject.y=this.$parent._ymouse;
		this.broadcastMessage(M_MOUSEMOVE,this,tempx,tempy);
	}
	private function checkWork():Boolean{
		if(this.$parent==undefined || this.$parent==null || this.$parent.toString()==undefined){
			return false;
		}
		return true;
	}
	public function Reset(){
		Mouse.removeListener(this.$mouseObject);
		this.$mouseObject=new Object();
		this.setMouseEvents();
	}
}